pub fn create_bb() {
    let x = 10;
    let num = || {
        println!("无参数，无返回值 闭包 => {}", x);
    };
    num();
}

pub fn create_bb1_1() {
    let name = String::from("zds");
    let add_name = || {
        let a1 = format!("{} {}", &name, "hi");
        a1
    };
    println!("{}", add_name());
    println!("name {}", name);
}

pub fn create_bb1_2() {
    let mut name = String::from("zds");
    let mut add_name = |s| {
        name.push_str(s);
    };
    add_name(" hi");
    println!("{}", name);
}

pub fn create_bb1_3() {
    let name = String::from("zds");
    let add_name = |s| {
        let mut name1 = name;
        name1.push_str(s);
        name1
    };
    println!("{}", add_name(" hi!!!"));
    // println!("{}", name); // 错误 name 被消费 所有权规则
}

pub fn create_bb2() {
    let y = 20;
    let num = |num| -> i32 { num + y };
    println!("有参数，有返回值 闭包 => {}", num(5))
}

pub fn create_bb3() {
    let name = "zds".to_string();
    let cont = || {
        println!("引用 捕获 => {}", name);
    };
    cont();
}

pub fn create_bb4() {
    let mut name = "zds".to_string();
    let mut cont = || {
        let n1 = &mut name;
        n1.push_str("hi");
        println!("可变引用 捕获 => {}", n1);
    };
    cont();
    cont();
    cont();
}

pub fn create_bb5() {
    let name = "zds".to_string();
    let cont = move || {
        let n1 = name;
        println!("所有权 捕获 => {}", n1);
    };
    cont();
    // cont(); // 报错
}

pub fn create_bb6_1<F>(f: F)
where
    F: Fn(),
{
    f()
}

pub fn create_bb6() {
    let x = 1;
    let num = || {
        println!("闭包 当参数 {}", x);
    };
    create_bb6_1(num)
}
